<?php
/**
 * 类别管理
 * @copyright reginx.com
 * $Id: cat.mod.php 129 2013-01-04 06:26:57Z reginx.com@gmail.com $
 */
class cat_mod extends admin_mod {

    /**
     * 分类类型
     *
     * @var unknown_type
     */
    public $ctype = 0;

    /**
     * cat table object
     *
     * @var unknown_type
     */
    public $obj = null;

    /**
     * 架构函数
     *
     * @param unknown_type $param
     */
    public function __construct($param){
        parent::__construct($param);
        $this->ctype = intval($this->get('ctype' , 'R'));
        $this->obj   = core::getobj('cat_tab');
        $this->cattype = config_lib::$ctype;
        $this->assign('ctype' , $this->ctype);
    }

    /**
     * 类别类别
     *
     */
    public function listAct(){
        $this->assign('list' , $this->obj->getslist( 0 , $this->ctype));
        $this->assign('atitle' , $this->cattype[$this->ctype]);
        $this->display('cat/list.html');
    }

    public function addAct(){
        $id = intval($this->get('id'));
        $this->assign('atitle' , '新增类别');
        if($id){
            $cat = $this->obj->where("id = {$id}")->get();
            $this->assign('cat' , $cat);
            $this->assign('atitle' , '修改 ' .$cat['name']);
        }
        $plist = $this->obj->getslist( 0 , $this->ctype , 1);
        $this->assign('plist' , $plist);
        $this->assign('ctype' , $this->ctype);
        $this->display('cat/add.html');
    }

    public function saveAct(){
        if($this->obj->load()){
            $ctype = $this->obj->data['ctype'];
            // refpath
            $oldpath = $newpath = ($this->obj->data['path'] ? $this->obj->data['path'] : '#0#') ;
            if($this->obj->data['parent']){
                $obj1 = new cat_tab();
                $parent = $obj1->where("id = {$this->obj->data['parent']}")->get();
                $newpath = $parent['path'] . $parent['id'] .'#';
            }else{
                $newpath = '#0#';
            }
            $this->obj->data['path'] = $newpath;
            $oldpath .= $this->obj->data['id'] . '#';
            $newpath .= $this->obj->data['id'] . '#';
            if($ret = $this->obj->save()){
                if($oldpath != $newpath){
                    $this->obj->refpath($ctype , $oldpath , $newpath );
                }
                $this->showmsg('操作成功!' , core::url('admin@cat-list-ctype-%d' , $ctype) , 1 , 2);
            }else{
                $this->showmsg($this->obj->geterr() , null , 1 , 2);
            }
        }else{
            $this->showmsg($this->obj->geterr() , null , 1 , 2);
        }
    }

}
?>